package cn.chenhongchao.zhaobiaopingtai.service.impl;

import cn.chenhongchao.zhaobiaopingtai.common.api.CommonPage;
import cn.chenhongchao.zhaobiaopingtai.dto.ReturnBidDTO;
import cn.chenhongchao.zhaobiaopingtai.entity.BusinessBid;
import cn.chenhongchao.zhaobiaopingtai.entity.ReturnBid;
import cn.chenhongchao.zhaobiaopingtai.mapper.BusinessBidMapper;
import cn.chenhongchao.zhaobiaopingtai.service.BusinessBidService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class BusinessBidServiceImpl extends ServiceImpl<BusinessBidMapper, BusinessBid> implements BusinessBidService {

    @Autowired
    private BusinessBidMapper businessBidMapper;


    @Override
    public CommonPage<BusinessBid> dynamicQuery(Integer pageNum, Integer pageSize, ReturnBidDTO returnBidDTO) {

        QueryWrapper<BusinessBid> wrapper = new QueryWrapper<>();

        // TODO tenderType 不知道对应哪个字段
//        if (!StringUtils.isEmpty(returnBidDTO.getTenderType())) {
//            wrapper.eq("commCode", returnBidDTO.getTenderType());
//        }1

        if (!StringUtils.isEmpty(returnBidDTO.getTenderNumber())) {
            wrapper.eq("tenderNumber", returnBidDTO.getTenderNumber());
        }

        if (!StringUtils.isEmpty(returnBidDTO.getTenderName())) {
            wrapper.like("tenderName", returnBidDTO.getTenderName());
        }

        if (!StringUtils.isEmpty(returnBidDTO.getCommCode())) {
            wrapper.like("commCode", returnBidDTO.getCommCode());
        }

        // 分页查询
        Page<BusinessBid> page = new Page<>(pageNum, pageSize);
        businessBidMapper.selectPage(page, wrapper);

        // 分装返回需要的类型
        CommonPage<BusinessBid> returnBidCommonPage = new CommonPage<BusinessBid>();
        returnBidCommonPage.setList(page.getRecords());

        Long pages = page.getPages();
        Integer total = Integer.valueOf(pages.toString());
        returnBidCommonPage.setTotalPage(total);

        returnBidCommonPage.setPageSize(pageSize);

//        Long current = page.getCurrent();
//        Integer num = Integer.valueOf(current.toString());
        returnBidCommonPage.setPageNum(pageNum);
        returnBidCommonPage.setTotal(page.getTotal());

        return returnBidCommonPage;
    }
}
